package com.hmall.search.Listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.hmall.api.client.ItemClient;
import com.hmall.api.domain.dto.ItemDTO;
import com.hmall.search.domain.po.ItemDoc;
import lombok.RequiredArgsConstructor;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RequiredArgsConstructor
public class ItemListener {

    private final RestHighLevelClient client = new RestHighLevelClient(RestClient.
            builder(HttpHost.create("http://192.168.80.128:9200")));

    private final ItemClient itemClient;

    //监听商品新增
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "search.item.index.queue", durable = "true"),
            exchange = @Exchange(name = "search.direct"),
            key = "item.index"
    ))
    public void listenItemIndex(Long id) throws IOException {
        //1.根据id去数据库查询最新的商品
        ItemDTO itemDTO = itemClient.queryItemById(id);
        if(itemDTO == null){
            return;
        }
        //2.解析为es需要的类型
        ItemDoc itemDoc = BeanUtil.copyProperties(itemDTO, ItemDoc.class);
        //3.创造请求条件
        IndexRequest request = new IndexRequest("items").id(itemDoc.getId());
        request.source(JSONUtil.toJsonStr(itemDTO), XContentType.JSON);
        //4.发送请求
        client.index(request, RequestOptions.DEFAULT);

    }

    //监听商品状态
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "search.item.status.queue", durable = "true"),
            exchange = @Exchange(name = "search.direct"),
            key = "item.updateStatus"
    ))
    public void listenItemStatus(Long id) throws IOException {
        //1.根据id去数据库查询最新的商品 已经在controller端改变status的商品
        ItemDTO itemDTO = itemClient.queryItemById(id);
        if(itemDTO == null){
            return;
        }
        //2.解析为es需要的类型
        ItemDoc itemDoc = BeanUtil.copyProperties(itemDTO, ItemDoc.class);
        //3.创造请求条件
        UpdateRequest request = new UpdateRequest("items",itemDoc.getId());
        request.doc(JSONUtil.toJsonStr(itemDTO), XContentType.JSON);
        //4.发送请求
        client.update(request, RequestOptions.DEFAULT);
    }

    //监听商品删除
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "search.item.delete.queue", durable = "true"),
            exchange = @Exchange(name = "search.direct"),
            key = "item.delete"
    ))
    public void listenItemDelete(Long id) throws IOException {

        // 1.准备Request对象
        DeleteRequest request = new DeleteRequest("items").id(id.toString());
        // 2.发送请求
        client.delete(request, RequestOptions.DEFAULT);

    }

}
